{{extends file="backend/layout.tpl"}} {{block name=title}}登录{{/block}} {{block
name=header}}
<style>
  body,
  html {
    height: 100%;
    background-color: gray;
  }

  .title {
    margin-bottom: 20px;
    text-align: center;
  }

  .main {
    width: 400px;
    margin: 100px auto;
    background-color: #fff;
    padding: 10px;
    border-radius: 10px;
  }

  .lk {
    text-align: center;
  }
</style>
{{/block}} {{block name=content}}

<body>
  <div class="main">
    <h1 class="title">登录</h1>
    <form class="layui-form" lay-filter="formDemo" action="POST">
      {{csrf_token}}
      <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
          <input
            type="text"
            name="username"
            required
            lay-verify="required"
            placeholder="请输入用户名"
            autocomplete="off"
            class="layui-input" />
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
          <input
            type="password"
            name="password"
            required
            lay-verify="required"
            placeholder="请输入密码"
            autocomplete="off"
            class="layui-input" />
        </div>
      </div>

      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">
            登录
          </button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>

    <div class="lk">
      <a href="/admin/register">没有账号？立马注册！</a>
    </div>
  </div>

  {{/block}} {{block name=script}}
  <script>
    //Demo
    layui.use("form", function() {
      var form = layui.form;
      var $ = layui.jquery;

      form.on("submit(formDemo)", function(dat) {
        // layer.msg(JSON.stringify(data.field));
        $.ajax({
          type: "POST",
          url: "login",
          data: dat.field,
          dataType: "json",
          success: function(response) {
            console.log(response);
            if (response.code == 1) {
              layer.msg(response.msg, {
                icon: 6
              });
              setTimeout(() => {
                window.location.href = '/admin'
              }, 500);
            } else {
              layer.msg(response.msg, {
                icon: 5
              });
            }
          },
        });
        return false;
      });
    });
  </script>
  {{/block}}